<?php


namespace app\controller\api;


use app\BaseController;
use app\services\area\RegionService;
use think\Request;

class RegionController extends BaseController
{
    /**
     * 获取省列表
     * @return \think\response\Json
     */
    public function getProvinceList() {
        try {
            $list = RegionService::getProvinceListService();
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 获取市列表
     * @param Request $request
     * @return \think\response\Json
     */
    public function getCityList(Request $request) {
        try {
            $provinceId = $request->param('province_id', 0);
            $name = $request->param('name', '', 'trim');
            $list = RegionService::getCityListByProvinceIdService($provinceId, $name);
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 获取区县列表
     * @param Request $request
     * @return \think\response\Json
     */
    public function getAreaList(Request $request) {
        try {
            $cityId = $request->param('city_id', 0);
            $name = $request->param('name', '', 'trim');
            $list = RegionService::getAreaListByCityIdService($cityId, $name);
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 根据市，区县获取街道
     * @param Request $request
     * @return \think\response\Json
     */
    public function getStreetList(Request $request) {
        try {
            $cityId = $request->param('city_id', 0);
            $areaId = $request->param('area_id', 0);
            $operatorId = $request->param('operator_id', 0);
            $franchiserId = $request->param('franchiser_id', 0);
            $list = RegionService::getStreetListService($cityId, $areaId, $operatorId, $franchiserId);
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }


    /**
     * 根据街道ID获取小区
     * @param Request $request
     * @return \think\response\Json
     */
    public function getCommunityList(Request $request) {
        try {
            $streetId = $request->param('street_id', 0);
            $list = RegionService::getCommunityListService($streetId);
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }
}